﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Niunan.VideoSys.Web.Areas.Adnn1n.Controllers
{
    public class VideoMXController : Controller
    {
        DAL.VideoDAL vdal = new DAL.VideoDAL();
        DAL.VideomxDAL mxdal = new DAL.VideomxDAL();



        public ActionResult Index(int videoid)
        {
            Model.Video v = vdal.GetModel(videoid);
            if (v == null)
            {
                return Content("无此视频");
            }
            ViewBag.videoname = v.title;
            return View();
        }

        /// <summary>
        /// 拼接条件
        /// </summary>
        /// <returns></returns>
        public string GetCond(int videoid)
        {

            string cond = $"videoid={videoid}";

            return cond;
        }

        /// <summary>
        /// 取总记录数
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTotalCount(int videoid)
        {
            int totalcount = mxdal.CalcCount(GetCond(videoid));
            return Content(totalcount.ToString());
        }

        /// <summary>
        /// 取分页数据，返回 JSON
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public ActionResult List(int pageindex, int pagesize, int videoid)
        {
            List<Model.Videomx> list = mxdal.GetListArray("*", "id asc", pagesize, pageindex, GetCond(videoid));
            ArrayList arr = new ArrayList();
            foreach (var item in list)
            {
                arr.Add(new
                {
                    id = item.id,
                    createtime = item.createtime.ToString("yyyy-MM-dd HH:mm"),
                    videoid = item.videoid,
                    title = item.title,
                    videopath = item.videopath,
                    visitnum = item.visitnum,

                });
            }
            return Json(arr);
        }



        // GET: Adnn1n/VideoMX
        public ActionResult Add(int videoid)
        {
            Model.Video v = vdal.GetModel(videoid);
            if (v == null)
            {
                return Content("没有对应视频。");
            }
            ViewBag.videoname = v.title;
            return View(v);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add(int videoid, string title, string videopath)
        {
            Model.Video v = vdal.GetModel(videoid);
            if (v == null)
            {
                return Content("没有对应视频。");
            }
            mxdal.Add(new Model.Videomx() { videoid = videoid, title = title, videopath = videopath });
            return Redirect("/adnn1n/video");
        }


        public ActionResult Delete(string ids)
        {
            try
            {
                int success = 0;
                string[] ss = ids.Split(',');
                foreach (var item in ss)
                {
                    int x;
                    if (int.TryParse(item, out x))
                    {
                        mxdal.Delete(x);
                        success++;
                    }
                }
                return Json(new { code = 0, msg = "成功删除" + success + "条记录！" });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = $"出错：{ex.Message}" });
            }
        }
    }
}